#include "usart.h"
#include "stm32f10x.h"

void usart1_init(void)
{   
	//����ʱ��
	 GPIO_InitTypeDef gpioStructure;
	 USART_InitTypeDef usartinitStructure;
	 NVIC_InitTypeDef NVICinitStructre;
	 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2);

	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,  ENABLE);
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO,   ENABLE);
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE);

	//����PA9,TX
	gpioStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	gpioStructure.GPIO_Pin =GPIO_Pin_9;
	gpioStructure.GPIO_Speed=GPIO_Speed_50MHz;
	
	GPIO_Init( GPIOA, &gpioStructure);//��ʼ��

	//����PA10,RX
	gpioStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	gpioStructure.GPIO_Pin=GPIO_Pin_10;
	
	GPIO_Init( GPIOA, &gpioStructure);//��ʼ��

	//���ô��ڽṹ��
	usartinitStructure.USART_BaudRate=115200;//������
	usartinitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//Ӳ����
	usartinitStructure.USART_Mode=USART_Mode_Rx  |  USART_Mode_Tx;//ģʽ
	usartinitStructure.USART_Parity=USART_Parity_No;//У��λ
	usartinitStructure.USART_StopBits=USART_StopBits_1;//ֹͣλ
	usartinitStructure.USART_WordLength=USART_WordLength_8b;//�ֽڳ���
	
  USART_Init(USART1, &usartinitStructure);//��ʼ��
  USART_ITConfig( USART1, USART_IT_RXNE ,  ENABLE);

  USART_Cmd(USART1, ENABLE );//�򿪴���
	
	NVICinitStructre.NVIC_IRQChannel=USART1_IRQn;
	NVICinitStructre.NVIC_IRQChannelCmd=ENABLE;
	NVICinitStructre.NVIC_IRQChannelPreemptionPriority=1;
	NVICinitStructre.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVICinitStructre );	
}


void usart2_init(void)
{   
	//����ʱ��
	 GPIO_InitTypeDef gpioStructure;
	 USART_InitTypeDef usartinitStructure;
	 NVIC_InitTypeDef NVICinitStructre;
	 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2);

	
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,  ENABLE);
	 RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO,   ENABLE);
	 RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);


	//����PA2,TX
	gpioStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	gpioStructure.GPIO_Pin =GPIO_Pin_2;
	gpioStructure.GPIO_Speed=GPIO_Speed_50MHz;
	
	GPIO_Init( GPIOA, &gpioStructure);//��ʼ��

	//����PA3,RX
	gpioStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	gpioStructure.GPIO_Pin=GPIO_Pin_3;
	
	GPIO_Init( GPIOA, &gpioStructure);//��ʼ��

	//���ô��ڽṹ��
	usartinitStructure.USART_BaudRate=115200;//������
	usartinitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//Ӳ����
	usartinitStructure.USART_Mode=USART_Mode_Rx  |  USART_Mode_Tx;//ģʽ
	usartinitStructure.USART_Parity=USART_Parity_No;//У��λ
	usartinitStructure.USART_StopBits=USART_StopBits_1;//ֹͣλ
	usartinitStructure.USART_WordLength=USART_WordLength_8b;//�ֽڳ���
	
  USART_Init(USART2, &usartinitStructure);//��ʼ��
  USART_ITConfig( USART2, USART_IT_RXNE ,  ENABLE);  //ʹ�ܽ����ж�

  USART_Cmd(USART2, ENABLE );//�򿪴���
	
	NVICinitStructre.NVIC_IRQChannel=USART2_IRQn;
	NVICinitStructre.NVIC_IRQChannelCmd=ENABLE;
	NVICinitStructre.NVIC_IRQChannelPreemptionPriority=1;
	NVICinitStructre.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVICinitStructre );
	
}



//�����ַ�
  void USARTSendByte (USART_TypeDef* USARTx, uint16_t Data)
{
		 USART_SendData( USARTx, Data);
     while( USART_GetFlagStatus( USARTx,  USART_FLAG_TXE)==RESET);//�ж����ݼĴ����Ƿ�Ϊ��
}
		
//�����ַ���	
  void USARTSendstr(USART_TypeDef* USARTx,  char *str)
{
		uint16_t i=0;
		do
		{
			 USARTSendByte( USARTx, *(str+i) );
       i++;
	  }while(*(str+i)!='\0');
		 while( USART_GetFlagStatus( USARTx,  USART_FLAG_TC)==RESET);
}
	
	
	
	
	



